package com.study.talk.module.controller;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.study.talk.common.resullt.FwResult;
import com.study.talk.module.entity.Moment;
import com.study.talk.module.entity.resp.MomentResponse;
import com.study.talk.module.entity.vo.MomentVo;
import com.study.talk.module.service.MomentCommentService;
import com.study.talk.module.service.MomentLikeService;
import com.study.talk.module.service.MomentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author thor
 * @since 2024-3-13
 */
@RestController
@RequestMapping("/moment")
public class MomentController {
    @Resource
    private MomentService momentService;

    @Resource
    private MomentLikeService momentLikeService;

    @Resource
    private MomentCommentService momentCommentService;


    @PostMapping("/add")
    @ApiOperation("新增动态")
    public FwResult<Boolean> addMoment(@RequestBody MomentVo momentVo){
        if (ObjectUtils.isNotNull(momentVo)){
            Moment moment = new Moment(momentVo);
            momentService.save(moment);
            return FwResult.ok(true);
        }
        return FwResult.failed();
    }

    @GetMapping("/get/{id}")
    @ApiOperation("根据id查询动态")
    public FwResult<Moment> getMoment(@PathVariable("id") Integer id){
        Moment moment = momentService.getById(id);
        if (ObjectUtils.isNotNull(moment)) return FwResult.ok(moment);
        else return FwResult.failed();
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除动态")
    public FwResult<Boolean> deleteMoment(@RequestBody Moment moment){
        boolean b = momentService.removeById(moment);
        if (b) return FwResult.ok(true);
        else return FwResult.failed(false);
    }

    @DeleteMapping("/update")
    @ApiOperation("修改动态")
    public FwResult<Boolean> updateMoment(@RequestBody Moment moment){
        boolean b = momentService.updateById(moment);
        if (b) return FwResult.ok(true);
        else return FwResult.failed();
    }


    @ApiOperation("获取所有好友的动态（包括自己）")
    @GetMapping("/get/friend/moment/{id}")
    public FwResult<ArrayList<MomentResponse>> getFriendMomentList(@PathVariable("id") Integer userId){
        return FwResult.ok(momentService.getFriendMomentList(userId));
    }


    @ApiOperation("获取自己的所有动态")
    @GetMapping("/get/my/moment/{id}")
    public FwResult<ArrayList<MomentResponse>> getMyMomentList(@PathVariable("id") Integer userId){
        return FwResult.ok(momentService.getMyMomentList(userId));
    }


}
